package com.dqhl.community.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dqhl.community.R;
import com.dqhl.community.model.ChatBean;
import com.dqhl.community.util.Config;

import org.xutils.x;

import java.util.List;

/**
 * Created by LIENLIN on 2016/9/8.
 */
public class ChatAdapter extends BaseAdapter {

    private List<ChatBean> list;
    private LayoutInflater inflater;
    private Context context;
    private String headImg;
    private OnPictureClickListener onPictureClickListener;

    public void setOnPictureClickListener(OnPictureClickListener onPictureClickListener) {
        this.onPictureClickListener = onPictureClickListener;
    }

    public ChatAdapter(Context context, List<ChatBean> list, String headImg) {
        this.context=context;
        this.list=list;
        this.headImg=headImg;
        inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        Log.d("ChatAdapter","list.size()="+list.size());
        return list.size();
    }

    @Override
    public byte[] getItem(int i) {
        if (list.get(i).content!=null){
            return list.get(i).content;
        }else {
            return null;
        }
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        ChatBean chat = list.get(i);
        String sendId = chat.sendId;
        Log.d("ChatAdapter","sendId="+sendId);
        final byte[] content = chat.content;//二进制
        if (sendId!=null){
            //发送
            view = inflater.inflate(R.layout.lv_chat_item_right, viewGroup, false);
            TextView mTvRight = (TextView) view.findViewById(R.id.tv_chat_item_right);
            ImageView mIvRight= (ImageView) view.findViewById(R.id.iv_chat_item_right);
            ImageView mIvRightTouXiang= (ImageView) view.findViewById(R.id.iv_chat_item_touxiang_right);
            //个人设置中没头像时，显示默认头像
            if (!TextUtils.isEmpty(headImg)){
                x.image().bind(mIvRightTouXiang, Config.Api.base_img_url+headImg);
            }
            if (content!=null){
                //图片
                mIvRight.setVisibility(View.VISIBLE);
                mTvRight.setVisibility(View.GONE);
                final Bitmap bitmap = BitmapFactory.decodeByteArray(content, 0, content.length);
                mIvRight.setImageBitmap(bitmap);
                mIvRight.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onPictureClickListener.showBigPicture(content);
                    }
                });
            }else {
                //文字
                mIvRight.setVisibility(View.GONE);
                mTvRight.setVisibility(View.VISIBLE);
                mTvRight.setText(chat.message);
            }
        }else {
            //接收
            view = inflater.inflate(R.layout.lv_chat_item_left, viewGroup, false);
            TextView mTvLeft = (TextView) view.findViewById(R.id.tv_chat_item_left);
            ImageView mIvLeft= (ImageView) view.findViewById(R.id.iv_chat_item_left);
            ImageView mIvLeftTouXiang= (ImageView) view.findViewById(R.id.iv_chat_item_touxiang_left);
            if (content!=null){
                //图片
                mIvLeft.setVisibility(View.VISIBLE);
                mTvLeft.setVisibility(View.GONE);
                final Bitmap bitmap = BitmapFactory.decodeByteArray(content, 0, content.length);
                mIvLeft.setImageBitmap(bitmap);
                mIvLeft.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onPictureClickListener.showBigPicture(content);
                    }
                });
            }else {
                //文字
                mIvLeft.setVisibility(View.GONE);
                mTvLeft.setVisibility(View.VISIBLE);
                mTvLeft.setText(chat.message);
            }
//            mTvLeft.setText(chat.message);
        }
        return view;
    }

    public void addList(ChatBean chat){
        list.add(chat);
        notifyDataSetChanged();
    }

    public interface OnPictureClickListener {
        void showBigPicture(byte[] bitmap);
    }
}
